﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Website
{
    public partial class LedigeTider : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtZipCode.Focus();
        }

        /// <summary>
        /// Function that is raised from client side and update GUI with available dates based on selected address/zipcode
        /// </summary>
        protected void callbackSelectDatePanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            try
            {
                // Instantiate data layer
                TireServiceDB db = new TireServiceDB();

                // Get value ZipCode
                string ZipCode = e.Parameter;

                // Get dates where we will be driving in zipcode area and where theres i free capacity in the future
                var AvailableDatesOnZipCode = from p in db.ServiceGeographicalAreaZipCodeLists
                                              join y in db.ServiceCarGeographicalAreaAllocations on p.ServiceGeoAreaId equals y.ServiceGeoAreaId into py
                                              from z in py.DefaultIfEmpty()
                                              join v in db.ServiceCarSchedules on
                                              new { Col1 = z.ServiceCarId, Col2 = z.ScheduleDate }
                                              equals
                                              new { Col1 = v.ServiceCarId, Col2 = v.ScheduleDate }
                                              into pv
                                              from x in pv.DefaultIfEmpty()
                                              // Filter parameters: ZipCode, Date, None GroupServiceDay, Available capacity
                                              where p.ZipCode == ZipCode & z.ScheduleDate > DateTime.Now.AddDays(1) & x.GroupServiceCarScheduleId == null & db.ServiceCarScheduleDetails.Where(q => q.ServiceCarId == z.ServiceCarId & q.ScheduleDate == z.ScheduleDate & q.ScheduleDetailTypeId == (int)Shared.ScheduleDetailType.Available).Count() > 0
                                              select new
                                              {
                                                  z.ScheduleDate
                                              };

                if (AvailableDatesOnZipCode.Count() == 0)
                {
                    throw new ArgumentException("Ingen ledige tider kunne findes. Venligst kontakt vores kundesupport for en tid.");
                }
                else
                {
                    List<DateTime> dates = new List<DateTime>();

                    foreach (var date in AvailableDatesOnZipCode.Distinct().ToList())
                    {
                        dates.Add(date.ScheduleDate);
                    }

                    ddlSelectDate.DataSource = dates;
                    ddlSelectDate.DataBind();
                }
            }
            catch (Exception ex)
            {

                // Error
                //throw new ArgumentException(ex.Message);
            }
        }

        /// <summary>
        /// Function that is raised from client side when date is selected and update GUI with timeslots based on selection
        /// </summary>
        protected void callbackSelectTimeslotPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            try
            {
                // Instantiate data layer
                TireServiceDB db = new TireServiceDB();

                // Get selected date
                DateTime date = Convert.ToDateTime(ddlSelectDate.Value);

                // Show timeslots
                var Timeslots = from p in db.ServiceCarScheduleDetails
                                join v in db.ServiceCarSchedules on
                                new { Col1 = p.ServiceCarId, Col2 = p.ScheduleDate }
                                equals
                                new { Col1 = v.ServiceCarId, Col2 = v.ScheduleDate }
                                into pv
                                from x in pv.DefaultIfEmpty()

                                join y in db.ServiceCarGeographicalAreaAllocations on
                                new { Col1 = x.ServiceCarId, Col2 = x.ScheduleDate }
                                equals
                                new { Col1 = y.ServiceCarId, Col2 = y.ScheduleDate }
                                into xy
                                from w in xy.DefaultIfEmpty()

                                join a in db.ServiceGeographicalAreaZipCodeLists on w.ServiceGeoAreaId equals a.ServiceGeoAreaId
                                into wa
                                from b in wa.DefaultIfEmpty()
                                // Filter parameters: ZipCode, Date, None GroupServiceDay, Available capacity
                                where b.ZipCode == txtZipCode.Text & p.ScheduleDate == date & x.GroupServiceCarScheduleId == null & p.ScheduleDetailTypeId == (int)Shared.ScheduleDetailType.Available
                                select new
                                {
                                    p.ScheduleDate,
                                    p.ServiceCarScheduleDetailId,
                                    p.StartTime,
                                    p.EndTime,

                                };

                var t = from p in Timeslots.ToList()
                        select new
                        {
                            p.ScheduleDate,
                            p.ServiceCarScheduleDetailId,
                            Interval = string.Format("{0:t}", p.StartTime) + " - " + string.Format("{0:t}", p.EndTime),
                            Fullday = "kl. 8 - 16"
                        };

                rblSelectTimeslot.DataSource = t.ToList();
                rblSelectTimeslot.ValueField = "ServiceCarScheduleDetailId";
                rblSelectTimeslot.TextField = "Interval";
                rblSelectTimeslot.DataBind();
            }
            catch (Exception ex)
            {
                // Error
                throw new ArgumentException(ex.Message);
            }
        }
    }
}